<template>
  <div class="app-container">
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <span>{{ this.name }}</span>
      <el-button size="mini" class="btn-add" @click="handleAdd()">新增目录</el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="homeAdvertiseTable"
        :data="list"
        style="width: 100%;"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border>
        <el-table-column label="标题" align="center">
          <template slot-scope="scope">{{ scope.row.name }}</template>
        </el-table-column>
        <el-table-column label="内容数量" align="center">
          <template slot-scope="scope">{{ scope.row.videoCount }}</template>
        </el-table-column>
        <el-table-column label="最近更新" align="center">
          <template slot-scope="scope">{{ scope.row.createTime | formatDateTime }}</template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleVideoList(scope.$index, scope.row)">内容管理
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      :title="isEdit?'编辑数据':'新增数据'"
      :visible.sync="dialogVisible"
      width="30%">
      <el-form
        :model="admin"
        ref="nameForm"
        :rules="rules"
        label-width="150px">
        <el-form-item label="请输入名称：" prop="name">
          <el-input class="input-width" v-model="admin.name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { deleteId, sourseVideoList, sourseVideoSave, sourseVideoUpdate, sourseVideoById,sourseVideoDeleteId } from '@/api/college'
import { formatDate } from '@/utils/date'
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  training: ''
}
const data = {
  'createTime': '',
  'name': '',
  'training': 0,
  'videoCount': '',
  'videoList': []
}
export default {
  name: 'CourseList',
  data () {
    return {
      row: {},
      addUms: false,
      isEdit: false,
      dialogVisible: false,
      name: '',
      admin: Object.assign({}, data),
      listQuery: Object.assign({}, defaultListQuery),
      typeOptions: Object.assign({}),
      list: null,
      total: null,
      listLoading: false,
      multipleSelection: [],
      operates: [
        {
          label: '删除',
          value: 0
        }
      ],
      operateType: null,
      courseTypeOptions: [],
      loading: false,
      deptOptions: [],
      rules: {
        name: [
          { required: true, message: '请输入', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.name = this.$route.query.name
    // this.getCourseType()
    this.getList()
  },
  filters: {
    formatDateTime (time) {
      if (time == null || time === '') {
        return 'N/A'
      }
      const date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  methods: {
    infoAdd () {
      this.$router.push({ path: '/college-ms/info' })
    },
    handleResetSearch () {
      this.listQuery = Object.assign({}, defaultListQuery)
    },
    handleSearchList () {
      this.listQuery.pageNum = 1
      this.getList()
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleSizeChange (val) {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.listQuery.pageNum = val
      this.getList()
    },
    handleDelete (index, row) {
      this.deleteCouerse(row.id)
    },
    handleVideoList (index, row) {
      this.$router.push({ path: '/college-ms/collegeVideo', query: { id: row.id } })
    },
    handleReview (index, row) {
      this.$router.push({ path: '/college-ms/collegeDetails', query: { id: row.id } })
    },
    handleAdd () {
      this.admin.name = ''
      this.admin.training = this.$route.query.id
      this.isEdit = false
      this.dialogVisible = true
    },
    handleUpdate (index, row) {
      this.admin.training = this.$route.query.id
      this.isEdit = true
      this.dialogVisible = true
      this.row = row
      sourseVideoById({ id: row.id }).then(response => {
        // this.course = response.data
        this.admin = Object.assign(this.admin, response.data)
      })
    },
    getList () {
      this.listQuery.training = this.$route.query.id
      sourseVideoList(this.listQuery).then(response => {
        console.log(this.listQuery)
        this.listLoading = false
        this.list = response.data.list
        this.total = response.data.total
      })
    },
    handleConfirm () {
      this.$refs['nameForm'].validate((valid) => {
        if (valid) {
          this.listLoading = true
          if (this.isEdit) {
            this.admin.id = this.row.id
            sourseVideoUpdate(this.admin).then(response => {
              this.dialogVisible = false
              this.listLoading = false
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              // this.$refs.tree.setCheckedKeys([])
              this.getList()
            })
          } else {
            this.admin.id = ''
            sourseVideoSave(this.admin).then(response => {
              this.dialogVisible = false
              this.listLoading = false
              this.$message({
                message: '提交成功',
                type: 'success'
              })
              this.getList()
            })
          }
        } else {
          this.$message({
            message: '提交失败',
            type: 'error',
            duration: 1000
          })
          return false
        }
      })
    },
    deleteCouerse (ids) {
      this.$confirm('是否要删除该课程?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = new URLSearchParams()
        params.append('id', ids)
        sourseVideoDeleteId(params).then(response => {
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      })
    }
  }
}
</script>
<style scoped>
  .input-width {
    /*width: 203px;*/
  }
</style>
